package com.CampusLife.controller;

import  com.CampusLife.bean.ProductType;
import com.CampusLife.bean.User;
import com.CampusLife.service.IUerService;
import com.CampusLife.until.ImagesUpload;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import  com.CampusLife.service.IProductTypeService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/productType")
public class ProductTypeController {
    @Resource
    private IProductTypeService iProductTypeService;
    @Resource
    private IUerService iUerService;

    /**
     *跳转到商品类型展示页面
     */
    @RequestMapping("/type")
    public String type(){
        return "redirect:/productType/query";
    }
       @RequestMapping("/query")
    public  String queryAll(Model model){
        List<ProductType> list=iProductTypeService.queryAll();
        model.addAttribute("list",list);
        return "admin/shoptypebasic";
    }

    /**
     *
     * @param model
     * @return
     * 跳转到后台商品展示页面
     */
    @RequestMapping("/queryshop")
    public  String queryAllshop(Model model){
        List<ProductType> list=iProductTypeService.queryAll();
        model.addAttribute("list",list);
        return "admin/shopbasic";
    }
    /**
     * 查询所有商品类型，给到商品增加页面
     * @param model
     * @return
     */
    @RequestMapping("/querytype")
    public  String queryAll1(Model model){
        List<ProductType> list=iProductTypeService.queryAll();
        model.addAttribute("list",list);
        return "home/addshop";
    }


    /**
     * 跳转到添加商品类型页面去
     * @return
     */
    @RequestMapping("gotoAdd")
    public String gatoAdd(){
        return "admin/addtypeshop";
    }
    @RequestMapping("/add")
    public String add( String type){
         ProductType productType = new ProductType();
         productType.setType(type);
        iProductTypeService.add(productType);
        return "redirect:/productType/query";
    }
    @RequestMapping("/byid")
    public String Byid(Integer t_id,Model model){
      ProductType productType=  iProductTypeService.queryById(t_id);
      model.addAttribute("list",productType);
        return "admin/shoptypeupdate";
    }

    @RequestMapping("/delete")
    public  String delete(ProductType productType){
         System.out.println(productType);
        iProductTypeService.delete(productType);
        return "redirect:/productType/query";
    }
    @RequestMapping("/update")
    public  String update(Integer t_id,String type){
        ProductType productType =new ProductType();
                  productType.setT_id(t_id);
                  productType.setType(type);
            iProductTypeService.update(productType);
            return "redirect:/productType/query";
    }
    @RequestMapping("/images")
    public String upload(@RequestParam("file") MultipartFile file,@RequestParam("username") String username)throws Exception{
        ImagesUpload image = new ImagesUpload();
       String path = image.Upload(file);
        User user=new User();
        user.setUsername(username);
        user.setPassword(path);
     iUerService.addUserName(user);
    return "admin/login";
    }
}
